package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author pengyehua
 * @date 2020/12/6 16:43
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService{

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 查询一级类目
     * @return
     */
    @Override
    public List<CategoryBO> queryIdAndName() {
        List<CategoryBO> categoryBOList = categoryMapper.selectIdAndName();
        return categoryBOList;
    }

    /**
     * 查询商品类类目信息
     * @return
     */
    @Override
    public List<CategoryL1> queryCategory() {
        List<CategoryL1> categoryL1s = categoryMapper.selectCategoryL1("L1");
        for (CategoryL1 categoryL1 : categoryL1s) {
            List<Category> categories = categoryMapper.selectCategory(categoryL1.getId());
            categoryL1.setChildren(categories);
        }
        return categoryL1s;
    }

    /**
     * 插入商品类目
     * @param category
     * @return
     */
    @Override
    public Category insertCategory(Category category) {
        CategoryExample categoryExample = new CategoryExample();
        byte sort_order =(byte) (categoryMapper.countByExample(categoryExample) + 1);
        Date date = new Date();
        category.setAddTime(date);
        category.setUpdateTime(date);
        category.setDeleted(false);
        category.setSortOrder(sort_order);
        categoryMapper.insertSelective(category);
        return category;
    }

    /**
     * 修改商品类目
     * @param category
     */
    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(new Date());
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    /**
     * 删除商品类目
     * @param category
     */
    @Override
    public void deleteCategory(Category category) {


        category.setDeleted(true);
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    /**
     * 查询指定一级类目下的二级类目个数
     * @param category
     */
    @Override
    public int queryL2Count(Category category) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(category.getId());
        int count =(int) categoryMapper.countByExample(categoryExample);
        return count;
    }

    /**
     * 查询二级类目下的商品数量
     * @param category
     * @return
     */
    @Override
    public int queryGoodsCount(Category category) {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andCategoryIdEqualTo(category.getId());
        int count =(int) goodsMapper.countByExample(goodsExample);
        return count;
    }

}
